
package com.ami.kvm.jviewer.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;

public class InfoDialog {
	public static final int WAIT_DIALOG = 0;
	public static final int INFORMATION_DIALOG = 1;
	public static final int ERROR_DIALOG = 2;
	public static final int CONFIRMATION_DIALOG = 3;
	public static final int UNDECORATED_DIALOG = 4;
	public static final int MODELESS_WAIT_DIALOG = 5;
	public static final int HOST_KBD_LANG = 0;
	public static final long ONE_SEC = 1000L;
	public static final long DISPLAY_DURATION = 5000L;
	private JDialog infoDialog;
	private static InfoDialog undecDialog;
	private String infoText;
	private long displayDuration;
	private WaitThread waitThread;
	private int dialogType;
	private String title;
	private JFrame ownerFrame;
	private JDialog ownerDialog;
	private int type;
	private JLabel durationMsgArea;

	public InfoDialog() {
		this.infoText = null;
		this.displayDuration = 5000L;

		this.dialogType = 0;
		this.title = null;
		this.ownerFrame = null;
		this.ownerDialog = null;
		this.type = 0;
		this.durationMsgArea = null;
	}

	private JDialog createInfoDialog() {
		try {
			int i = 0;
			int j = 0;
			Object localObject = null;

			if ((this.ownerFrame != null) && (this.ownerFrame.isShowing())) {
				this.infoDialog = new JDialog(this.ownerFrame, this.title, false);
				localObject = this.ownerFrame;

			} else if ((this.ownerDialog != null) && (this.ownerDialog.isShowing())) {
				this.infoDialog = new JDialog(this.ownerDialog, this.title, false);
				localObject = this.ownerDialog;
			} else {
				this.infoDialog = new JDialog(JViewer.getMainFrame(), this.title, false);
				localObject = JViewer.getMainFrame();
			}
			if (this.dialogType == 0) {
				this.infoDialog.setMinimumSize(new Dimension(240, 80));
				this.infoDialog.setModal(true);
				this.infoDialog.setUndecorated(true);
			} else if ((this.dialogType == 4) || (this.dialogType == 5)) {
				this.infoDialog.setMinimumSize(new Dimension(240, 80));
				this.infoDialog.setModal(false);
				this.infoDialog.setUndecorated(true);
			} else {
				this.infoDialog.setMinimumSize(new Dimension(240, 120));
				this.infoDialog.setModal(false);
			}
			this.infoDialog.add(getinfoPane());
			this.infoDialog.pack();
			this.infoDialog.setResizable(false);
			this.infoDialog.requestFocus();
			i = (((Component) localObject).getLocationOnScreen().x + ((Component) localObject).getWidth()
					- this.infoDialog.getWidth()) / 2;
			j = (((Component) localObject).getLocationOnScreen().y + ((Component) localObject).getHeight()
					- this.infoDialog.getHeight()) / 2;
			this.infoDialog.setLocation(i, j);
			this.infoDialog.setLocationRelativeTo((Component) localObject);
			this.infoDialog.addWindowListener(new DiaologWindowListener());
			this.waitThread = new WaitThread();
			this.infoDialog.setVisible(true);
		} catch (Throwable localThrowable) {
			Debug.out.println(localThrowable);
		}
		return this.infoDialog;
	}

	private JPanel getMessagePane() {
		JPanel localJPanel = null;
		JLabel localJLabel = null;
		String str1 = "&nbsp;&nbsp;&nbsp;";
		String str2 = "<p style=\"padding-top:5;\">";
		String str3 = "</p>";
		try {
			if (this.infoText.contains("\n")) {
				this.infoText = this.infoText.replace("\n", str1 + "<br>" + str2 + str1);
			}
			localJLabel = new JLabel("<html>" + str2 + str1 + this.infoText + str1 + str3 + "</html>");
			localJLabel.setHorizontalAlignment(0);
			localJLabel.setVerticalAlignment(0);
			localJLabel.setVerticalTextPosition(0);
			localJPanel = new JPanel(new BorderLayout());
			localJPanel.add(localJLabel, "Center");
			if (this.dialogType == 3) {
				this.durationMsgArea = new JLabel();
				this.durationMsgArea.setHorizontalAlignment(0);
				this.durationMsgArea.setVerticalAlignment(0);
				localJPanel.add(this.durationMsgArea, "South");
			}
		} catch (Throwable localThrowable) {
			Debug.out.println(localThrowable);
		}
		return localJPanel;
	}

	public JLabel getDurationMsgArea() {
		return this.durationMsgArea;
	}

	private JPanel getOKButton() {
		JPanel localJPanel = null;
		JButton localJButton = null;
		if (localJButton == null) {
			localJButton = new JButton(LocaleStrings.getString("A_3_GLOBAL"));
			localJButton.setSize(100, 25);
			localJButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
					InfoDialog.this.closeDialog();
				}
			});
			localJButton.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent paramAnonymousKeyEvent) {
					if (paramAnonymousKeyEvent.getKeyCode() == 10) {
						InfoDialog.this.closeDialog();
					}
				}
			});
			localJPanel = new JPanel();
			localJPanel.add(Box.createHorizontalGlue());
			localJPanel.add(localJButton);
			localJPanel.add(Box.createHorizontalGlue());
		}
		return localJPanel;
	}

	private JPanel getYesNoButton() {
		JPanel localJPanel = null;
		JButton localJButton1 = null;
		JButton localJButton2 = null;
		if (localJButton1 == null) {
			localJButton1 = new JButton(LocaleStrings.getString("A_7_GLOBAL"));
			localJButton1.setSize(100, 25);
			localJButton1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
					InfoDialog.this.closeDialog();
					JViewerApp.getInstance().confirmationDialogResponse(InfoDialog.this.type);
				}
			});
			localJButton1.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent paramAnonymousKeyEvent) {
					if (paramAnonymousKeyEvent.getKeyCode() == 10) {
						InfoDialog.this.closeDialog();
						JViewerApp.getInstance().confirmationDialogResponse(InfoDialog.this.type);
					}
				}
			});
		}
		if (localJButton2 == null) {
			localJButton2 = new JButton(LocaleStrings.getString("A_8_GLOBAL"));
			localJButton2.setSize(100, 25);
			localJButton2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
					InfoDialog.this.closeDialog();
				}
			});
			localJButton2.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent paramAnonymousKeyEvent) {
					if (paramAnonymousKeyEvent.getKeyCode() == 10) {
						InfoDialog.this.closeDialog();
					}
				}
			});
		}
		localJPanel = new JPanel();
		localJPanel.add(Box.createHorizontalGlue());
		localJPanel.add(Box.createVerticalStrut(40));
		localJPanel.add(localJButton1);
		localJPanel.add(localJButton2);
		localJPanel.add(Box.createHorizontalGlue());
		return localJPanel;
	}

	private JPanel getinfoPane() {
		JPanel localJPanel = null;
		try {
			localJPanel = new JPanel();
			localJPanel.setLayout(new BorderLayout());
			localJPanel.add(getMessagePane(), "Center");
			switch (this.dialogType) {
			case 1:
			case 2:
				localJPanel.add(getIconPanel(), "Before");
				localJPanel.add(getOKButton(), "South");
				break;
			case 0:
			case 4:
			case 5:
				localJPanel.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.GRAY));
				break;
			case 3:
				localJPanel.add(getIconPanel(), "Before");
				localJPanel.add(getYesNoButton(), "South");
			}
		} catch (Throwable localThrowable) {
			Debug.out.println(localThrowable);
		}
		return localJPanel;
	}

	private JPanel getIconPanel() {
		JPanel localJPanel = null;
		JLabel localJLabel = null;
		String str = null;
		URL localURL = null;
		try {
			localJPanel = new JPanel();
			localJLabel = new JLabel();
			switch (this.dialogType) {
			case 1:
				str = "res/information.png";
				break;
			case 2:
				str = "res/error.png";
				break;
			default:
				str = "res/information.png";
			}
			if (str != null)
				localURL = JViewer.class.getResource(str);
			if (localURL != null)
				localJLabel.setIcon(new ImageIcon(localURL));
			localJLabel.setHorizontalAlignment(0);
			localJLabel.setVerticalAlignment(0);
			localJPanel.add(localJLabel);
		} catch (Throwable localThrowable) {
			Debug.out.println(localThrowable);
		}
		return localJPanel;
	}

	private void initialize() {
		try {
			createInfoDialog();
		} catch (Throwable localThrowable) {
			Debug.out.println(localThrowable);
		}
	}

	public static void showDialog(Component paramComponent, String paramString, long paramLong) {
		InfoDialog localInfoDialog = new InfoDialog();
		setOwner(localInfoDialog, paramComponent);
		localInfoDialog.infoText = paramString;
		localInfoDialog.displayDuration = paramLong;
		localInfoDialog.dialogType = 0;
		localInfoDialog.initialize();
	}

	public static InfoDialog showDialog(Component paramComponent, String paramString, long paramLong, int paramInt) {
		InfoDialog localInfoDialog = new InfoDialog();
		setOwner(localInfoDialog, paramComponent);
		localInfoDialog.infoText = paramString;
		localInfoDialog.displayDuration = paramLong;
		localInfoDialog.dialogType = paramInt;
		localInfoDialog.initialize();
		return localInfoDialog;
	}

	public static void showDialog(Component paramComponent, String paramString1, String paramString2, int paramInt) {
		InfoDialog localInfoDialog = new InfoDialog();
		setOwner(localInfoDialog, paramComponent);
		localInfoDialog.infoText = paramString1;
		localInfoDialog.dialogType = paramInt;
		localInfoDialog.displayDuration = 0L;
		localInfoDialog.title = paramString2;
		localInfoDialog.initialize();
	}

	public static void showDialog(Component paramComponent, String paramString1, String paramString2, int paramInt1,
			long paramLong, int paramInt2) {
		InfoDialog localInfoDialog = new InfoDialog();
		setOwner(localInfoDialog, paramComponent);
		localInfoDialog.infoText = paramString1;
		localInfoDialog.dialogType = paramInt1;
		localInfoDialog.displayDuration = paramLong;
		localInfoDialog.title = paramString2;
		localInfoDialog.type = paramInt2;
		localInfoDialog.initialize();
	}

	public static void showDialog(Component paramComponent, String paramString, int paramInt) {
		if (undecDialog == null) {
			undecDialog = new InfoDialog();
			setOwner(undecDialog, paramComponent);
			undecDialog.infoText = paramString;
			undecDialog.dialogType = paramInt;
			undecDialog.displayDuration = 0L;
			undecDialog.initialize();
		}
	}

	public static void undecDialogClose() {
		if (undecDialog != null) {
			undecDialog.closeDialog();
			undecDialog = null;
		}
	}

	private void closeDialog() {
		if (this.waitThread != null) {
			if (this.dialogType == 3) {
				this.displayDuration = 0L;
			} else {
				synchronized (this.waitThread) {
					this.waitThread.notify();
				}
			}
			this.waitThread = null;
		} else if (this.infoDialog != null) {
			this.infoDialog.dispose();
			this.infoDialog = null;
		}
	}

	public static void setOwner(InfoDialog paramInfoDialog, Component paramComponent) {
		try {
			if (paramComponent == null) {
				return;
			}

			if ((paramComponent.getClass().equals(Class.forName("javax.swing.JFrame")))
					|| (paramComponent.getClass().getGenericSuperclass().equals(Class.forName("javax.swing.JFrame")))) {
				paramInfoDialog.ownerFrame = ((JFrame) paramComponent);

			} else if ((paramComponent.getClass().equals(Class.forName("javax.swing.JDialog"))) || (paramComponent
					.getClass().getGenericSuperclass().equals(Class.forName("javax.swing.JDialog")))) {
				paramInfoDialog.ownerDialog = ((JDialog) paramComponent);
			} else {
				setOwner(paramInfoDialog, paramComponent.getParent());
			}
		} catch (ClassNotFoundException localClassNotFoundException) {
			Debug.out.println(localClassNotFoundException);
		}
	}

	class WaitThread extends Thread {
		int wokeup = 0;

		public WaitThread() {
			start();
		}

		public void run() {
			if (InfoDialog.this.infoDialog == null) {
				return;
			}
			if (InfoDialog.this.dialogType == 3) {
				do {
					try {
						if (this.wokeup == 0) {
							synchronized (InfoDialog.this.waitThread) {
								this.wokeup = 1;
							}
						}
						InfoDialog.this.durationMsgArea.setText(LocaleStrings.getString("M_2_ID")
								+ new Integer((int) (InfoDialog.this.displayDuration / 1000L)).toString()
								+ LocaleStrings.getString("M_3_ID"));
						InfoDialog.this.durationMsgArea.repaint();
						sleep(1000L);
						InfoDialog.this.displayDuration = (InfoDialog.this.displayDuration - 1000L);
					} catch (InterruptedException e) {
						Debug.out.println(e);
					}
				} while (InfoDialog.this.displayDuration > 0L);
			} else {
				try {
					synchronized (InfoDialog.this.waitThread) {
						InfoDialog.this.waitThread.wait(InfoDialog.this.displayDuration);
						InfoDialog.this.displayDuration = 0L;
					}
				} catch (InterruptedException localInterruptedException) {
					Debug.out.println(localInterruptedException);
				}
			}
			if ((InfoDialog.this.infoDialog != null) && (InfoDialog.this.displayDuration <= 0L)) {
				if ((InfoDialog.this.dialogType == 5) && (JViewerApp.getInstance().getHidInitDialog() != null)) {
					JViewerApp.getInstance().setHidInitDialog(null);
					JViewerApp.getInstance().getRCView().addKMListeners();
				}

				InfoDialog.this.infoDialog.dispose();
				InfoDialog.this.infoDialog = null;
			}
		}
	}

	class DiaologWindowListener extends WindowAdapter {
		DiaologWindowListener() {
		}

		public void windowClosing(WindowEvent paramWindowEvent) {
			InfoDialog.this.closeDialog();
		}
	}

	public static InfoDialog getUndecDialog() {
		return undecDialog;
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\gui\InfoDialog.
 * class Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */